package cn.jji8.createX.function.fill;

import cn.jji8.createX.Main;
import cn.jji8.patronSaint.quick.boxesMenu.CanSavedMenuManger;
import cn.jji8.patronSaint.quick.boxesMenu.ClickImplement;
import cn.jji8.patronSaint.quick.boxesMenu.MenuManger;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class fill implements ClickImplement {
    MenuManger menu;
    FillManger fillManger;

    public fill(Player player) {
        //新的菜单
        MenuManger menu = new CanSavedMenuManger(new File(Main.getMain().getDataFolder(),"menu/fill/"+player.getName()),6,"§g填充工具");

        //填充按钮
        //因为FillManger会在玩家打开的时候自动根据情况给自己填充按钮，所以不需要给他设置按钮物品了。
        menu.setSingleClick(49,fillManger = new FillManger(menu));

        //设置挖掘工具
        ClickImplement clickImplement = new ClickImplement() {
            @Override
            public void click(InventoryClickEvent inventoryClickEvent) {
                inventoryClickEvent.setCancelled(true);
                if(!(inventoryClickEvent.getWhoClicked() instanceof Player)){
                    return;
                }
                //获取玩家光标上的物品
                ItemStack Cursor = inventoryClickEvent.getCursor();
                if(Cursor==null){
                    Cursor = new ItemStack(Material.AIR);
                }
                ItemStack itemStack2 = new ItemStack(Cursor);
                inventoryClickEvent.getWhoClicked().getOpenInventory().setCursor(fillManger.getMiningTools());
                fillManger.setMiningTools(itemStack2);
                fillManger.refreshMiningTools();
            }
        };
        menu.setSingleClick(47,clickImplement);
        menu.setSingleClick(51,clickImplement);




        //填充按钮左边的
        ItemStack itemStack = new ItemStack(Material.GLASS);
        ItemMeta itemMeta = itemStack.getItemMeta();
        assert itemMeta != null;
        itemMeta.setDisplayName("§d未选择点1");
        itemStack.setItemMeta(itemMeta);
        menu.setItem(53,itemStack);
        menu.setSingleClick(53, new ClickImplement() {
            @Override
            public void click(InventoryClickEvent e) {
                e.setCancelled(true);
            }
        });
        //填充按钮右边的
        itemStack = new ItemStack(Material.GLASS);
        itemMeta = itemStack.getItemMeta();
        assert itemMeta != null;
        itemMeta.setDisplayName("§d未选择点2");
        itemStack.setItemMeta(itemMeta);
        menu.setItem(45,itemStack);
        menu.setSingleClick(45, new ClickImplement() {
            @Override
            public void click(InventoryClickEvent e) {
                e.setCancelled(true);
            }
        });
        //填充按钮中间的
        itemStack = new ItemStack(Material.GLASS_PANE);
        itemMeta = itemStack.getItemMeta();
        assert itemMeta != null;
        itemMeta.setDisplayName("§d将需要填充的方块放到此处");
        itemStack.setItemMeta(itemMeta);
        menu.setItem(46,itemStack);
        menu.setItem(48,itemStack);
        menu.setItem(50,itemStack);
        menu.setItem(52,itemStack);
        ClickImplement clickImplement1 = new ClickImplement(){
            @Override
            public void click(InventoryClickEvent e) {
                e.setCancelled(true);
                ItemStack itemStack1 =  e.getCursor();
                if(itemStack1==null){
                    itemStack1 = new ItemStack(Material.AIR);
                }
                ItemStack Cursor = new ItemStack(itemStack1);
                Cursor.setAmount(1);
                ItemMeta itemMeta1 = Cursor.getItemMeta();
                if(itemMeta1!=null){
                    List<String> list1 = new ArrayList<>();
                    list1.add("§g（将会填此方块）");
                    itemMeta1.setLore(list1);
                    Cursor.setItemMeta(itemMeta1);
                }
                Material CursorType = Cursor.getType();
                if(Cursor.getType().isAir()){
                    ItemStack itemStack = new ItemStack(Material.GLASS_PANE);
                    ItemMeta itemMeta = itemStack.getItemMeta();
                    assert itemMeta != null;
                    itemMeta.setDisplayName("空气");
                    List<String> list = new ArrayList<>();
                    list.add("§g（将会填充空气）");
                    itemMeta.setLore(list);
                    itemStack.setItemMeta(itemMeta);
                    Cursor =itemStack;
                    CursorType =null;
                }else if(!Cursor.getType().isBlock()){
                    ItemStack itemStack = new ItemStack(Material.RED_STAINED_GLASS_PANE);
                    ItemMeta itemMeta = itemStack.getItemMeta();
                    assert itemMeta != null;
                    itemMeta.setDisplayName("§d此物品不是一个可放置的方块");
                    List<String> list = new ArrayList<>();
                    list.add("§g（将会填充空气）");
                    itemMeta.setLore(list);
                    itemStack.setItemMeta(itemMeta);
                    Cursor =itemStack;
                    CursorType =null;
                }

                menu.setItem(46,Cursor);
                menu.setItem(48,Cursor);
                menu.setItem(50,Cursor);
                menu.setItem(52,Cursor);
                fillManger.setFillBookMaterial(CursorType);
            }
        };
        menu.setSingleClick(46,clickImplement1);
        menu.setSingleClick(48,clickImplement1);
        menu.setSingleClick(50,clickImplement1);
        menu.setSingleClick(52,clickImplement1);

        //返回初始化好的menu
        this.menu = menu;
    }

    /**
     * 处理点击事件的方法，实现后由MenuManger调用
     *
     */
    @Override
    public void click(InventoryClickEvent e) {
        e.setCancelled(true);
        if(e.getWhoClicked() instanceof Player){
            menu.open((Player) e.getWhoClicked());
        }
    }
}
